<?php

namespace App\Repository;

use App\Model\Admin;
use App\Validator\AdminValidator;

class AdminRepository extends Repository
{

    protected $admin;
    protected $vaildator;

    public function __construct()
    {
        parent::__construct();

        $this->admin = new Admin();
        $this->validator =new AdminValidator();
    }

    public function show()
    {
        $admin = $this->admin->where('username', $this->params['username'])->first();

        return $admin;
    }

    public function getAdmin()
    {
        $admin = $this->admin->first();

        return $admin;
    }

    public function addAdmin()
    {
        $admin = $this->getAdmin();

        if($admin) {
            return ['code' => '404', 'message' => '站长已存在'];
        }

        $vaildator = $this->validator->validatorAdd($this->params);

        if(false == $vaildator) {
            return ['code' => '404', 'message' => '参数有误'];
        }

        $options = [
            'cost' => 12,
        ];

        $this->admin->mobile = $this->params['mobile'];
        $this->admin->username = $this->params['username'];
        $this->admin->password = password_hash($this->params['password'], PASSWORD_DEFAULT, $options);

        $this->admin->save();

        return $this->admin;
    }

}
